package com.qqbot.impl.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qqbot.core.message.MessageChain;
import com.qqbot.impl.service.LingDuApiService;
import com.qqbot.impl.utils.Constants;
import com.qqbot.impl.utils.HttpClientUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 零度API服务
 *
 * @author changwenpeng
 * @date 2022年5月14日
 */
@Service
public class LingDuApiServiceImpl implements LingDuApiService {

    @Override
    public MessageChain jieXi(String keyword, Long qq) throws Exception {
        String url = "https://www.0do.cn/jiexi.html";
        JSONObject body = new JSONObject();
        body.put("url", keyword);
        String resJson = HttpClientUtils.postJson(url, "application/json", "UTF-8", null, body.toJSONString(), 10000, 10000);
        if (StringUtils.hasText(resJson)) {
            JSONObject json = JSONObject.parseObject(resJson);
            String code = json.getString("code");
            String msg = json.getString("msg");
            if (Constants.HTTP_200.equals(code)) {
                JSONObject data = json.getJSONObject("data");
                String title = data.getString("title");
                String downImg = data.getString("download_image");
                String downUrl = data.getString("url");
                String url1 = "https://shengapi.cn/api/dwz.php?url=" + downUrl;
                String resTxt = HttpClientUtils.get(url1, 10000, 10000);
                return new MessageChain().at(qq).text(msg + "\n")
                        .text("标题：" + title + "\n")
                        .image(downImg)
                        .text("下载链接：" + resTxt);
            }
        }
        return new MessageChain().text("解析失败！");
    }

}
